layui.config({
    base : "/dhjdglxt/modules/util/"//自定义加载的模块
}).use(['form','layer','jquery','layedit','laydate','laypage','table','commUtil','element','treetable'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;
    var table = layui.table;
    var commUtil = layui.commUtil;
    var element = layui.element;
    var treetable = layui.treetable;//很重要
    //导入
    layui.use(['upload'],function(){
        var upload = layui.upload;
        var recId=$("#recId").val();
        console.log("---recId------"+recId)

        upload.render({
            elem : '#onImportGcwtHbxx',
            method: 'post',
            url : basePath+'/service/dbRecord/uploadHbxx?recId='+recId,
            accept : 'file',
            exts: 'xls|xlsx',
            done : function(res) {
                layer.msg("导入成功",{icon:0});
                CloseWin();
            }
        });

        upload.render({
            elem : '#onImportJdndjh',
            method: 'post',
            url : basePath+'/service/dbRecord/uploadJdndjh?recId='+recId,
            accept : 'file',
            exts: 'xls|xlsx',
            done : function(res) {
                layer.msg("导入成功",{icon:0});
                CloseWin();
            }
        });


    });
    
    $("#onEditGcwtHbxx").click(function () {
        var buttonTxt=$("#onEditGcwtHbxx").text();
        if("编辑模式"===buttonTxt){
            $("#onEditGcwtHbxx").text("取消编辑模式");
            $("#onEditGcwtHbxx").addClass("layui-btn-danger");
            $("#countTableDiv").css("display","none");
            $("#editTableDiv").css("display","block");

            $("#countTableZzjDiv").css("display","none");
            $("#editTableZzjDiv").css("display","block");

        }else {
            $("#onEditGcwtHbxx").text("编辑模式");
            $("#onEditGcwtHbxx").removeClass("layui-btn-danger");
            $("#editTableDiv").css("display","none");
            $("#countTableDiv").css("display","block");

            $("#editTableZzjDiv").css("display","none");
            $("#countTableZzjDiv").css("display","block");
            var recId=$("#recId").val();
            $.ajax({
                url: basePath + "/service/dbRecord/dbNewInfo/"+recId,
                type: "GET",
                data: {},
                dataType:"json",
                contentType : 'application/json;charset=utf-8', //设置请求头信息
                success: function (result) {
                    console.log("----result-------"+JSON.stringify(result))
                    if(result.code==0){
                        let dbNewInfoA=result.data.dbNewInfoA;
                        let lineNum=0;
                        $(".editTr").each(function () {
                            var lineType=$(this).find('td:first').find("input").val();
                            if("all"===lineType){
                                $(this).find('td:eq(2)').find("input").val(dbNewInfoA[lineNum].gjFxxs);
                                $(this).find('td:eq(3)').find("input").val(dbNewInfoA[lineNum].gjBhs);
                                $(this).find('td:eq(4)').find("input").val(dbNewInfoA[lineNum].gjHs);
                            }else {
                                $(this).find('td:eq(1)').find("input").val(dbNewInfoA[lineNum].unitPrice);
                                $(this).find('td:eq(2)').find("input").val(dbNewInfoA[lineNum].flightHours);
                                $(this).find('td:eq(3)').find("input").val(dbNewInfoA[lineNum].hjBhs);
                                $(this).find('td:eq(4)').find("input").val(dbNewInfoA[lineNum].hjHs);
                            }
                            lineNum++;
                        });
                        let lineNumView=0;
                        $(".viewTr").each(function () {
                            var lineType=$(this).find('td:eq(1)').text();
                            if("共计"===lineType){
                                $(this).find('td:eq(2)').text(dbNewInfoA[lineNumView].gjFxxs);
                                $(this).find('td:eq(3)').text(dbNewInfoA[lineNumView].gjBhs);
                                $(this).find('td:eq(4)').text(dbNewInfoA[lineNumView].gjHs);
                            }else {
                                $(this).find('td:eq(1)').text(dbNewInfoA[lineNumView].unitPrice);
                                $(this).find('td:eq(2)').text(dbNewInfoA[lineNumView].flightHours);
                                $(this).find('td:eq(3)').text(dbNewInfoA[lineNumView].hjBhs);
                                $(this).find('td:eq(4)').text(dbNewInfoA[lineNumView].hjHs);
                            }
                            lineNumView++;
                        });

                        /*周转件*/
                        let dbNewInfoZzj=result.data.dbNewInfoZzj;
                        let lineZzjNum=0;
                        $(".editZzjTr").each(function () {
                            var lineType=$(this).find('td:first').find("input").val();
                            if("all"===lineType){
                                $(this).find('td:eq(2)').find("input").val(dbNewInfoZzj[lineZzjNum].gjFxxs);
                                $(this).find('td:eq(3)').find("input").val(dbNewInfoZzj[lineZzjNum].gjBhs);
                                $(this).find('td:eq(4)').find("input").val(dbNewInfoZzj[lineZzjNum].gjHs);
                            }else {
                                $(this).find('td:eq(1)').find("input").val(dbNewInfoZzj[lineZzjNum].unitPrice);
                                $(this).find('td:eq(2)').find("input").val(dbNewInfoZzj[lineZzjNum].flightHours);
                                $(this).find('td:eq(3)').find("input").val(dbNewInfoZzj[lineZzjNum].hjBhs);
                                $(this).find('td:eq(4)').find("input").val(dbNewInfoZzj[lineZzjNum].hjHs);
                            }
                            lineZzjNum++;
                        });
                        let lineZzjNumView=0;
                        $(".viewZzjTr").each(function () {
                            var lineType=$(this).find('td:eq(1)').text();
                            if("共计"===lineType){
                                $(this).find('td:eq(2)').text(dbNewInfoZzj[lineZzjNumView].gjFxxs);
                                $(this).find('td:eq(3)').text(dbNewInfoZzj[lineZzjNumView].gjBhs);
                                $(this).find('td:eq(4)').text(dbNewInfoZzj[lineZzjNumView].gjHs);
                            }else {
                                $(this).find('td:eq(1)').text(dbNewInfoZzj[lineZzjNumView].unitPrice);
                                $(this).find('td:eq(2)').text(dbNewInfoZzj[lineZzjNumView].flightHours);
                                $(this).find('td:eq(3)').text(dbNewInfoZzj[lineZzjNumView].hjBhs);
                                $(this).find('td:eq(4)').text(dbNewInfoZzj[lineZzjNumView].hjHs);
                            }
                            lineZzjNumView++;
                        });

                    }
                }
            });
        }


    });
    var onEditSave=true;
    $("#onEditSave").click(function () {
        if(onEditSave){
            onEditSave=false;
            let dataArr=[];
            let lineNum=0;
            $(".editTr").each(function () {
                /*let ss= $(this).parent().parent().parent().find(".layui-hide").find(".layui-table-cell").text();*/
                //var tds=$(this).find('.editTd');
                var tds=$(this).find('td');
                let dataJson={};
                tds.each(function () {
                    var val=$(this).find("input").val();
                    var name=$(this).find("input").attr("name");
                    dataJson[name]=val;
                });
                //var lineType=$(this).find('td:first').find("input").val();
                //console.log("------lineType---"+lineType);
                dataJson.dataType="3";
                dataJson.tableType="2";
                dataJson.orderNum=lineNum;
                lineNum++;
                var recId=$("#recId").val();
                dataJson.recordId=recId;
                dataArr.push(dataJson);
            });
            let dataStr=JSON.stringify(dataArr);
            console.log("--dataStr---"+dataStr);

            $.ajax({
                url: basePath + "/service/dbRecord/editHbxx",
                type: "POST",
                data: dataStr,
                dataType:"json",
                contentType : 'application/json;charset=utf-8', //设置请求头信息
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg("保存成功",{icon:0});
                        onEditSave=true;
                    } else {
                        layer.msg(result.msg, {icon: 2})
                        onEditSave=true;
                    }
                }
            });
        }

    });
    /*还原数据*/
    $("#onEditRevert").click(function () {
        var recId=$("#recId").val();
        $.ajax({
            url: basePath + "/service/dbRecord/revertHbxx/"+recId,
            type: "GET",
            data: {
                tableType:"2"
            },
            dataType:"json",
            contentType : 'application/json;charset=utf-8', //设置请求头信息
            success: function (result) {
                if (result.code == 0) {
                    layer.msg("数据还原成功",{icon:0});
                } else {
                    layer.msg(result.msg, {icon: 2})
                }
            }
        });
    });

    /*---------------------------*/
    var onEditZzjSave=true;
    $("#onEditZzjSave").click(function () {
        if(onEditZzjSave){
            onEditZzjSave=false;
            let dataArr=[];
            let lineNum=0;
            $(".editZzjTr").each(function () {
                /*let ss= $(this).parent().parent().parent().find(".layui-hide").find(".layui-table-cell").text();*/
                //var tds=$(this).find('.editTd');
                var tds=$(this).find('td');
                let dataJson={};
                tds.each(function () {
                    var val=$(this).find("input").val();
                    var name=$(this).find("input").attr("name");
                    dataJson[name]=val;
                });
                //var lineType=$(this).find('td:first').find("input").val();
                //console.log("------lineType---"+lineType);
                dataJson.dataType="3";
                dataJson.tableType="3";
                dataJson.orderNum=lineNum;
                lineNum++;
                var recId=$("#recId").val();
                dataJson.recordId=recId;
                dataArr.push(dataJson);
            });
            let dataStr=JSON.stringify(dataArr);
            console.log("--dataStr---"+dataStr);

            $.ajax({
                url: basePath + "/service/dbRecord/editHbxx",
                type: "POST",
                data: dataStr,
                dataType:"json",
                contentType : 'application/json;charset=utf-8', //设置请求头信息
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg("保存成功",{icon:0});
                        onEditZzjSave=true;
                    } else {
                        layer.msg(result.msg, {icon: 2})
                        onEditZzjSave=true;
                    }
                }
            });
        }

    });
    /*还原数据*/
    $("#onEditZzjRevert").click(function () {
        var recId=$("#recId").val();
        $.ajax({
            url: basePath + "/service/dbRecord/revertHbxx/"+recId,
            type: "GET",
            data: {
                tableType:"3"
            },
            dataType:"json",
            contentType : 'application/json;charset=utf-8', //设置请求头信息
            success: function (result) {
                if (result.code == 0) {
                    layer.msg("数据还原成功",{icon:0});
                } else {
                    layer.msg(result.msg, {icon: 2})
                }
            }
        });
    });
    //监听保存
    form.on('submit(saveSubmit)', function(data){
        console.log("-----data.field-----"+JSON.stringify(data.field));
        $.ajax({
            type: "PUT",
            url: basePath+'/service/dbRecord/'+data.field.recId,
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    parent.location.reload();
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });
    });


})

